added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / block.cs
bloba8a74eb29f4bb79db099a88c77502d0c0f4e5c96
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using System;
19 using System.Collections;
20 using System.Reflection;
21 using System.Reflection.Emit;
23 public sealed class Block : AST{
24 private Completion completion;
25 private ArrayList list;
27 internal Block(Context context)
28 : base(context) {
29 this.completion = new Completion();
30 this.list = new ArrayList();
33 internal void Append(AST elem){
34 this.list.Add(elem);
37 internal void ComplainAboutAnythingOtherThanClassOrPackage(){
38 for (int i = 0, n = this.list.Count; i < n; i++){
39 Object elem = this.list[i];
40 if (elem is Class || elem is Package || elem is Import) continue;
41 Block b = elem as Block;
42 if (b != null && b.list.Count == 0) continue;
43 Expression e = elem as Expression;
44 if (e != null && e.operand is AssemblyCustomAttributeList) continue;
45 ((AST)elem).context.HandleError(JSError.OnlyClassesAndPackagesAllowed);
46 return;
50 internal override Object Evaluate(){
51 this.completion.Continue = 0;
52 this.completion.Exit = 0;
53 this.completion.value = null;
54 for (int i = 0, n = this.list.Count; i < n; i++){
55 AST elem = (AST)(this.list[i]);
56 Object val;
57 try{
58 val = elem.Evaluate();
59 }catch(JScriptException e){
60 if (e.context == null){
61 e.context = elem.context;
63 throw e;
65 Completion c = (Completion)val;
66 if (c.value != null)
67 this.completion.value = c.value;
68 if (c.Continue > 1){
69 this.completion.Continue = c.Continue - 1;
70 break;
72 if (c.Exit > 0){
73 this.completion.Exit = c.Exit - 1;
74 break;
76 if (c.Return)
77 return c;
79 return this.completion;
82 internal void EvaluateStaticVariableInitializers(){
83 for (int i = 0, n = this.list.Count; i < n; i++){
84 Object elem = this.list[i];
85 VariableDeclaration vard = elem as VariableDeclaration;
86 if (vard != null && vard.field.IsStatic && !vard.field.IsLiteral){
87 vard.Evaluate();
88 continue;
90 StaticInitializer sinit = elem as StaticInitializer;
91 if (sinit != null){
92 sinit.Evaluate();
93 continue;
95 Class cl = elem as Class;
96 if (cl != null){
97 cl.Evaluate();
98 continue;
100 Constant cnst = elem as Constant;
101 if (cnst != null && cnst.field.IsStatic){
102 cnst.Evaluate();
103 continue;
105 Block block = elem as Block;
106 if (block != null)
107 block.EvaluateStaticVariableInitializers();
111 internal void EvaluateInstanceVariableInitializers(){
112 for (int i = 0, n = this.list.Count; i < n; i++){
113 Object elem = this.list[i];
114 VariableDeclaration vard = elem as VariableDeclaration;
115 if (vard != null && !vard.field.IsStatic && !vard.field.IsLiteral){
116 vard.Evaluate();
117 continue;
119 Block block = elem as Block;
120 if (block != null)
121 block.EvaluateInstanceVariableInitializers();
125 internal override bool HasReturn(){
126 for (int i = 0, n = this.list.Count; i < n; i++){
127 AST elem = (AST)(this.list[i]);
128 if (elem.HasReturn())
129 return true;
131 return false;
134 internal void ProcessAssemblyAttributeLists(){
135 for (int i = 0, n = this.list.Count; i < n; i++){
136 Expression expr = this.list[i] as Expression;
137 if (expr != null){
138 AssemblyCustomAttributeList acl = expr.operand as AssemblyCustomAttributeList;
139 if (acl != null) acl.Process();
144 internal void MarkSuperOKIfIsFirstStatement(){
145 if (this.list.Count > 0 && this.list[0] is ConstructorCall)
146 ((ConstructorCall)this.list[0]).isOK = true;
149 internal override AST PartiallyEvaluate(){
150 for (int i = 0, n = this.list.Count; i < n; i++){
151 AST elem = (AST)(this.list[i]);
152 this.list[i] = elem.PartiallyEvaluate();
154 return this;
157 internal Expression ToExpression(){
158 if (this.list.Count == 1 && this.list[0] is Expression)
159 return (Expression)this.list[0];
160 else
161 return null;
164 internal override void TranslateToIL(ILGenerator il, Type rtype){
165 //This assumes that rtype == Void.class
166 Label lab = il.DefineLabel();
167 compilerGlobals.BreakLabelStack.Push(lab);
168 compilerGlobals.ContinueLabelStack.Push(lab);
169 for (int i = 0, n = this.list.Count; i < n; i++){
170 AST elem = (AST)(this.list[i]);
171 elem.TranslateToIL(il, Typeob.Void);
173 il.MarkLabel(lab);
174 compilerGlobals.BreakLabelStack.Pop();
175 compilerGlobals.ContinueLabelStack.Pop();
178 internal override void TranslateToILInitializer(ILGenerator il){
179 for (int i = 0, n = this.list.Count; i < n; i++){
180 AST elem = (AST)(this.list[i]);
181 elem.TranslateToILInitializer(il);
185 internal void TranslateToILInitOnlyInitializers(ILGenerator il){
186 for (int i = 0, n = this.list.Count; i < n; i++){
187 Constant c = this.list[i] as Constant;
188 if (c != null) c.TranslateToILInitOnlyInitializers(il);
192 internal void TranslateToILInstanceInitializers(ILGenerator il){
193 for (int i = 0, n = this.list.Count; i < n; i++){
194 AST elem = (AST)(this.list[i]);
195 if (elem is VariableDeclaration && !((VariableDeclaration)elem).field.IsStatic && !((VariableDeclaration)elem).field.IsLiteral){
196 elem.TranslateToILInitializer(il);
197 elem.TranslateToIL(il, Typeob.Void);
198 }else if (elem is FunctionDeclaration && !((FunctionDeclaration)elem).func.isStatic)
199 elem.TranslateToILInitializer(il);
200 else if (elem is Constant && !((Constant)elem).field.IsStatic)
201 elem.TranslateToIL(il, Typeob.Void);
202 else if (elem is Block)
203 ((Block)elem).TranslateToILInstanceInitializers(il);
207 internal void TranslateToILStaticInitializers(ILGenerator il){
208 for (int i = 0, n = this.list.Count; i < n; i++){
209 AST elem = (AST)(this.list[i]);
210 if (elem is VariableDeclaration && ((VariableDeclaration)elem).field.IsStatic ||
211 elem is Constant && ((Constant)elem).field.IsStatic){
212 elem.TranslateToILInitializer(il);
213 elem.TranslateToIL(il, Typeob.Void);
214 }else if (elem is StaticInitializer)
215 elem.TranslateToIL(il, Typeob.Void);
216 else if (elem is FunctionDeclaration && ((FunctionDeclaration)elem).func.isStatic)
217 elem.TranslateToILInitializer(il);
218 else if (elem is Class)
219 elem.TranslateToIL(il, Typeob.Void);
220 else if (elem is Block)
221 ((Block)elem).TranslateToILStaticInitializers(il);
225 internal override Context GetFirstExecutableContext(){
226 for (int i = 0, n = this.list.Count; i < n; i++){
227 AST elem = (AST)(this.list[i]);
228 Context ctx;
229 if ((ctx = elem.GetFirstExecutableContext()) != null)
230 return ctx;
232 return null;